﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace CSharpXDL
{//http://iso.esd.microsoft.com/WCPDL/BD1B8A49393E30CC9C4E5C88457D73E964F1F3B18/Windows8-ConsumerPreview-64bit-ChineseSimplified.iso
    internal class Rate
    {
        long length = 0;
	long download = 0;
	long undownload = 0;
	long mRate = 0;
	int calcSec=3;
	String rate;
	String completed;
	String timeRemain ;
    DataGridViewCellCollection cells;
	List<BlockDownloader> list;
	DownLoad downloader;
	FileStream bprser;
    BinaryWriter bprWriter;
	int ingRowIndex;
	int completedIndex;
    Dictionary<char, long> bprMap;
	public Rate(DownLoad downloader1, List<BlockDownloader> list2,
			long length2){

		downloader=downloader1;
		this.length = length2;
		this.list = list2;
        bprMap = downloader.bprMap;
        bprser = downloader.Bprser;
        bprWriter = new BinaryWriter(bprser);
        foreach (BlockDownloader bdownloader in  list) {
			download += bdownloader.length();
		}
        cells = downloader1.DataGridViewRow.Cells;
		/*Record.addDownloading(downloader.args);
		for (BlockDownloader bdownloader : list) {
			download += bdownloader.length();
		}
		nf.setMinimumFractionDigits(2);
		bprser=downloader.bprser;*/
	}
    public void run()
    { 
        while(true){

            	if(downloader.completed){
                    bprWriter.Close();
                    File.Delete(downloader.bprFile);
                    downloader.xDLForm.Dling.Rows.RemoveAt(downloader.DataGridViewRow.Index);
                    downloader.xDLForm.CompleteView.Rows.Add(new Object[] {downloader.args[1] });
                   // downloader..DataGridView.Rows.Remove(downloader.DataGridViewRow);
					return;
				}
    		    bprWriter.Seek(0,SeekOrigin.Begin);
                long t = 0;
				foreach (BlockDownloader ddownloader in list) {
                    bprMap.Remove(ddownloader.fName);
                    bprMap.Add(ddownloader.fName, ddownloader.readed);
                    bprWriter.Write(ddownloader.fName);
                    bprWriter.Write(ddownloader.readed);
                    t += ddownloader.readed;
				}
                bprWriter.Flush();
                
                undownload = length - t;
                mRate = (t - download) / calcSec;
                download = t;
                if (mRate == 0) continue;
				float tcompleted = (float) ((double) download / (double) length) * 100;
				double need_time = undownload / mRate;
				timeRemain = (int) (need_time / 3600) + "小时"
												+ (int) (need_time % 3600) / 60 + "分钟"
												+ (int) (need_time % 60)+"秒";
                this.completed = "" + Math.Round(tcompleted, 1) + "%";
                rate =""+(mRate / 1024);

                Console.WriteLine("completed: " + completed + " Rate: " + rate + "k/s" + " need time:" + timeRemain);
                cells[0].Value = downloader.args[1];
                cells[1].Value = rate + "k/s";
                cells[2].Value = timeRemain;
	
                //Console.WriteLine("download:" + t + " length:" + length+" " + (download / length) + "%");
                    Thread.Sleep(1000 * calcSec);
        }
    }

        //ftp://dygod1:dygod1@d615.dygod.org:5008/孤岛惊魂.[国语中字.1024分辨率]/[电影天堂-www.dy2018.net]孤岛惊魂HD国语中字.rmvb    
    }
}
